﻿using System;
using System.Linq;
using Myotragus.Data.Metadata;
using Myotragus.Data.Containers;
using Myotragus.Data.Util;

namespace Myotragus.Data.Hbm
{
	public abstract class LocalizableEntityHbmGenerator : AutomaticHbmGenerator,
		ILocalizableEntityHbmGenerator
	{
		protected LocalizableEntityHbmGenerator(IEntitiesContainer entities, 
			ILocalizableEntityMapping mapping)
			: base(entities, mapping)
		{
			Mapping = mapping;
		}

		protected new ILocalizableEntityMapping Mapping { get; private set; }

		protected override string GenerateNonIdFieldsMappings(Type interfaceType)
		{
			return string.Concat(
					Mapping.GetLocalizableProperties(interfaceType)
						.Select(p => GenerateLocalizableField(p)).Join("\r\n"),
					Mapping.GetNonLocalizableProperties(interfaceType)
						.Select(p => GenerateAutoFieldMapping(p)).Join("\r\n")
				);
		}

		protected abstract string GenerateLocalizableField(IPropertyMapping p);
	}
}